package com.zhdl.network.modbusrtu.after;

import cn.hutool.core.util.HexUtil;
import com.project.command.instruction.strategy.abs.AbsAfterControlCommand;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.zhdl.modules.tools.web.eventbus.center.EventBusCenter;
import com.zhdl.network.modbusrtu.dto.ModbusDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service(ModbusAfterService.MODBUS_AFTER_SERVICE)
public class ModbusAfterService extends AbsAfterControlCommand<ModbusDto, ByteBuf> {
    public static final String MODBUS_AFTER_SERVICE = "modbusAfterService";

    /**
     * 后置处理结果判断
     *
     * @param resp
     * @return
     */
    @Override
    public boolean afterResultJudge(AfterStrategyParam resp) {
        /*
            判断当前线程以及执行的哪一个after
         */
        log.debug("执行MOTOR_AFTER_SERVICE modbusAfterService");
        log.info(Thread.currentThread().getName() + "------------------");
        //此处接收串口或网口客户端回应代码,并处理
        List<ModbusDto> list = resp.getNewData();
        ModbusDto motorDto = list.get(0);
        String instruction = resp.getInstruction();
        EventBusCenter.getEventBus().post(resp);
        log.info("COM" + resp.getPortKey() + "发送指令>>>>" + instruction + "收到指令》》》》" + HexUtil.encodeHexStr(motorDto.getFullBuff()));
        return true;
    }

    /**
     * 判断 读取 3200 状态字数据
     */
    public String getMotorStatus(ModbusDto modbusDto) {
        byte[] unitBuff = modbusDto.getUnitBuff();
        return HexUtil.encodeHexStr(unitBuff);
    }

}
